package com.jeesite.modules.kj.api.controller.activity;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.enumCode.UnicomResponseEnums;
import com.jeesite.modules.kj.api.vo.DataListPageVo;
import com.jeesite.modules.kj.api.vo.response.ActivityCommentResponseVo;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.api.wechatUtils.TokenUtils;
import com.jeesite.modules.kj.entity.KjActivityComment;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.exception.FreezMemberException;
import com.jeesite.modules.kj.service.KjActivityCommentService;
import com.jeesite.modules.kj.service.KjActivityService;
import com.jeesite.modules.kj.service.KjContributeProjectService;
import com.jeesite.modules.kj.utils.KjMemberUtil;
import com.jeesite.modules.kj.utils.LikesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:
 */
@Api(value = "活动评论api", tags = {"活动API"})
@Slf4j
@Controller
@RequestMapping(value = "/api/activityComment")
public class ActivityCommentApiController {
    private final static Logger logger = LoggerFactory.getLogger(ActivityCommentApiController.class);
    @Autowired
    KjActivityService kjActivityService;

    @Autowired
    KjContributeProjectService kjContributeProjectService;
    @Autowired
    KjActivityCommentService kjActivityCommentService;

    @ApiOperation(value = "获取活动评论列表", notes = "获取活动评论列表")
    @GetMapping(value = "list")
    public ResponseBean<DataListPageVo<ActivityCommentResponseVo>> list(String activityId, int pageNo, int pageSize, HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("Authorization");
        KjActivityComment where = new KjActivityComment();
        where.setActivityId(activityId);
        where.getSqlMap().getWhere().andBracket("process_status", QueryType.EQ, KjActivityComment.ACCEPT);

        if(StringUtils.isNotBlank(token)) {
            String memberId = TokenUtils.verify(token);
            if(memberId != null) {
                where.getSqlMap().getWhere().or("member_id", QueryType.EQ, memberId);
            }
        }
        where.getSqlMap().getWhere().endBracket();

        Page page = new Page(pageNo, pageSize);
        where.setPage(page);
        Page<KjActivityComment> results = kjActivityCommentService.findPage(where);
        DataListPageVo<ActivityCommentResponseVo> vos = new DataListPageVo<>(results, ActivityCommentResponseVo.class);
        if(StringUtils.isNotBlank(token)) {
            String memberId = TokenUtils.verify(token);
            List<ActivityCommentResponseVo> lists = ListUtils.newArrayList();
            for (ActivityCommentResponseVo vo: vos.getList()
            ) {
                vo.setIsLike(LikesUtil.getIsLike(vo.getId(), memberId));
                lists.add(vo);
            }
            vos.setList(lists);
        }
        return new ResponseBean<>(vos);
    }


    @ApiOperation(value = "添加评论", notes = "添加评论")
    @PostMapping(value = "create")
    @UserLoginToken
    public ResponseBean<String> create(@ApiIgnore @CurrentUser String memberId, String activityId, String commentType, String content) throws FreezMemberException {
        KjActivityComment comment = new KjActivityComment();
        KjMember currentMember = null;
        currentMember = KjMemberUtil.getCurrentMember(memberId);

        comment.setActivityId(activityId);
        comment.setCommentType(commentType);
        comment.setKjMember(currentMember);
        String activityName = "";
        if(commentType.equals("ACTIVITY")){
            activityName = kjActivityService.get(activityId).getName();
        }else if(commentType.equals("PROJECT")){
            activityName = kjContributeProjectService.get(activityId).getName();
        }
        comment.setActivityName(activityName);
        comment.setContent(content);
        comment.setCommentTime(new Date());
        kjActivityCommentService.save(comment);
        return new ResponseBean<>("保存成功");
    }



    @ApiOperation(value = "删除评论", notes = "删除评论")
    @PostMapping(value = "delete")
    @UserLoginToken
    public ResponseBean<String> delete(@ApiIgnore @CurrentUser String memberId, String id) {
        KjActivityComment comment = kjActivityCommentService.get(id);
        KjMember currentMember = null;
        try {
            currentMember = KjMemberUtil.getCurrentMember(memberId);
        } catch (FreezMemberException e) {
            return new ResponseBean<>(Global.FALSE, UnicomResponseEnums.NO_USER_EXIST);
        }
        if(currentMember != null && comment.getKjMember().getId().equals(currentMember.getId())){
            kjActivityCommentService.delete(comment);
            return new ResponseBean<>("删除成功");
        }else{
            return new ResponseBean<>(Global.FALSE, UnicomResponseEnums.NO_PERMISSION);
        }

    }
}
